package com.spancer.gateway.autoFilter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

/**
 * author by: mars
 * Date: 2020/11/6 15:59
 * Description: 自定义过滤器
 */
@Component
@Slf4j
public class PreLogGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {


    @Override
    public GatewayFilter apply(NameValueConfig config) {
        return ((exchange, chain)-> {
            log.debug("请求进来了....{},{}",config.getName(),config.getValue());
            // 修改请求
            ServerHttpRequest modifiedRequest = exchange.getRequest().mutate().build();
            // 将修改的请求传入
            ServerWebExchange modifiedExchange = exchange.mutate().request(modifiedRequest).build();
            return chain.filter(modifiedExchange);
        });
    }
}
